<template>
  <div class="container right-panel-active">
    <!-- 注册 -->
    <div class="container_form container--signup">
      <div  class="form" id="form1">
        <h2 class="form_title">注册</h2>
        <label>
          <input type="text" v-model="signUpParam.account" placeholder="账号" class="input"/>
        </label>
        <label>
          <input type="email" v-model="signUpParam.email" placeholder="邮箱" class="input"/>
        </label>
        <label>
          <input type="password" v-model="signUpParam.password" placeholder="密码" class="input"/>
        </label>
        <input type="button" class="btn" value="注册" @click="toCheckSignUp()"/>
      </div>
    </div>

    <!-- 登录 -->
    <div class="container_form container--signin">
      <div class="form" id="form2">
        <h2 class="form_title">登陆</h2>
        <label>
          <input type="text" v-model="loginParam.account" placeholder="账号" class="input"/>
        </label>
        <label>
          <input type="password" v-model="loginParam.password" placeholder="密码" class="input"/>
        </label>
        <a href="https://www.baidu.com/" class="link">忘记密码</a>
        <input type="button" class="btn" value="登陆" @click="toLogin()"/>
      </div>
    </div>

    <!-- 浮层 -->
    <div class="container_overlay">
      <div class="overlay">
        <div class="overlay_panel overlay--left">
          <button class="btn" id="signIn">登陆</button>
        </div>
        <div class="overlay_panel overlay--right">
          <button class="btn" id="signUp">注册</button>
        </div>
      </div>
    </div>

    <!-- 验证码弹窗 -->
    <el-dialog v-model="dialogFormVisible" title="请输入验证码">
    <el-form>
      <el-form-item label="验证码" :label-width="formLabelWidth">
        <el-input v-model="signUpParam.verificationCode" placeholder="验证码"  autocomplete="off" />
        <el-button type="warning">重新获取验证码</el-button>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="toSignUp">
          注册
        </el-button>
      </span>
    </template>
  </el-dialog>
  </div>

  <!-- 背景 -->
  <div class="slidershow">
    <div id="slidershow_image_1" class="slidershow--image" ></div>
    <div id="slidershow_image_2" class="slidershow--image"></div>
    <div id="slidershow_image_3" class="slidershow--image"></div>
    <div id="slidershow_image_4" class="slidershow--image"></div>
  </div>

  <!-- partial -->
</template>

<script>
import {translational_loginBox} from '@/assets/js/get_func'
import {onBeforeMount} from "vue"
import {checkSignUp, login, signUp} from "@/request/api/login";
import {TOKEN} from "@/common/constants";
import router from "@/router";
import { ElMessage } from 'element-plus';

export default {
  data() {
    // 设置登陆参数
    const loginParam = {
      account: '',  // 账号
      password: ''  // 密码
    }
    // 注册参数
    const signUpParam = {
      account: '',  // 账号
      email: '',  // 邮箱
      password: '',  // 密码
      verificationCode: ''  // 验证码
    }
    const from = {
      delivery: false,
    }
    const dialogFormVisible = false
    const formLabelWidth = '140px'
    return {loginParam, signUpParam, from, dialogFormVisible, formLabelWidth}
  },
  methods: {
    // 执行登陆请求
    toLogin() {
      // 提取到登陆时用户输入的账号和密码
      let account = this.loginParam.account;
      let password = this.loginParam.password;
      // 如果账号和密码为空则不执行
      if (!account || !password) {
        ElMessage.error('请输入账号和密码');
        return;
      }
      // 判定输入的账号和密码是否符合要求
      let accountLoginFlag = (account.length < 5 || account.length > 20);
      let passwordLoginFlag = (password.length < 6 || password > 16);
      if (accountLoginFlag || passwordLoginFlag) {
        ElMessage.warning('输入的账号和密码不符合规则')
        if (accountLoginFlag) {ElMessage.warning('账号的长度在 5 - 20 之间');}
        if (passwordLoginFlag) {ElMessage.warning('密码的长度在 6 - 16 之间');}
        return;
      }
      login(account, password).then(res => {
        // 如果返回的数据不为空则表示请求成功
        if (res !== null && res.data !== null) {
          // 添加 token
          localStorage.setItem(TOKEN, res.data);
          // 跳转到首页
          router.push('/')
        }
      })
    },
    // 注册按钮点击事件
    // 对数据进行验证
    toCheckSignUp() {
      // 获取到账号密码和邮箱
      let account = this.signUpParam.account;
      let email = this.signUpParam.email;
      let password = this.signUpParam.password;
      if (!account || !password || !email) {
        ElMessage.error('请输入账号、密码和邮箱');
        return;
      }
      // 判定输入的账号和密码是否符合要求
      let accountLoginFlag = (account.length < 5 || account.length > 20);
      let passwordLoginFlag = (password.length < 6 || password > 16);
      if (accountLoginFlag || passwordLoginFlag) {
        ElMessage.warning('输入的账号和密码不符合规则')
        if (accountLoginFlag) {ElMessage.warning('账号的长度在 5 - 20 之间');}
        if (passwordLoginFlag) {ElMessage.warning('密码的长度在 6 - 16 之间');}
        return;
      }
      // 邮箱验证
      if (email.search(/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/)) {
        ElMessage.warning('请输入正确的邮箱')
        return;
      }
      // 发送验证注册请求
      checkSignUp(account, email, password, this.signUpParam.verificationCode).then(res => {
        if (res != null && res.data != null) {
          // 弹出验证码的输入框
          this.dialogFormVisible = true;
        }
      })
    },
    toSignUp() {
      console.log('注册请求')
      let account = this.signUpParam.account;
      let email = this.signUpParam.email;
      let password = this.signUpParam.password
      let verificationCode = this.signUpParam.verificationCode;
      signUp(account, email, password, verificationCode).then(res => {
        if (res != null && res.data != null) {
          // 关闭验证码弹窗
          this.dialogFormVisible = false;
          // 弹出注册成功消息
          ElMessage.success('注册成功')
        }
      })
    }
  },
  setup() {
    onBeforeMount(() => {
      translational_loginBox()
    })
  }
}

</script>

<style scoped>
/*验证码弹窗样式*/
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
/*验证码弹窗样式 end*/
:root {
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
  Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}


.form_title {
  font-weight: 300;
  margin: 0 0 1.25rem;
}

.link {
  color: #333;
  font-size: 0.9rem;
  margin: 1.5rem 0;
  text-decoration: none;
}

.container {
  place-items: center;
  vertical-align: center;
  align-items: center;
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  display: grid;
  box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25),
  0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
  border-radius: 0.7rem;
  height: 420px;
  max-width: 758px;
  overflow: hidden;
  position: fixed;
  width: 100%;
  top: 20%;
  left: 30%;
  z-index: 99;
}

.container_form {
  height: 100%;
  position: absolute;
  top: 0;
  transition: all 0.6s ease-in-out;
}

.container--signin {
  left: 0;
  width: 50%;
  z-index: 5;
}

.container.right-panel-active .container--signin {
  transform: translateX(100%);
}

.container--signup {
  left: 0;
  opacity: 0;
  width: 50%;
  z-index: 4;
}

.container.right-panel-active .container--signup {
  -webkit-animation: show 0.6s;
  animation: show 0.6s;
  opacity: 1;
  transform: translateX(100%);
  z-index: 8;
}

.container_overlay {
  height: 100%;
  left: 50%;
  overflow: hidden;
  position: absolute;
  top: 0;
  transition: transform 0.6s ease-in-out;
  width: 50%;
  z-index: 100;
}

.container.right-panel-active .container_overlay {
  transform: translateX(-100%);
}

.overlay {
  background-color: rgba(255, 255, 255, 0.25);
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  left: -100%;
  position: relative;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 200%;
}

.container.right-panel-active  .overlay {
  transform: translateX(50%);
}

.overlay_panel {
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;
  position: absolute;
  text-align: center;
  top: 0;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 50%;
}

.overlay--left {
  transform: translateX(-20%);
}

.container.right-panel-active .overlay--left {
  transform: translateX(0);
}

.overlay--right {
  right: 0;
  transform: translateX(0);
}

.container.right-panel-active > .overlay--right {
  transform: translateX(20%);
}

.btn {
  background-color: #095C91;
  background-image: linear-gradient(90deg, #095C91 0%, #0393A3 74%);
  border-radius: 20px;
  border: 0.2px solid #315A74;
  color: white;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: bold;
  letter-spacing: 0.1rem;
  padding: 0.9rem 4rem;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
}

.form > .btn {
  margin-top: 1.5rem;
}

.btn:active {
  transform: scale(0.95);
}

.btn:focus {
  outline: none;
}

.form {
  background-color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 3rem;
  height: 100%;
  text-align: center;
}

.input {
  background-color: #f5f5f5;
  border: none;
  padding: 0.9rem 0.9rem;
  margin: 0.5rem 0;
  width: 100%;
  border-radius: 2%;
}

@-webkit-keyframes show {
  0%,
  49.99% {
    opacity: 0;
    z-index: 4;
  }

  50%,
  100% {
    opacity: 1;
    z-index: 8;
  }
}

.slidershow {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.slidershow--image {
  position: absolute;
  width: 100%;
  height: 100%;
  background: no-repeat 50% 50%;
  background-size: cover;
  -webkit-animation-name: kenburns;
  animation-name: kenburns;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  -webkit-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
  -webkit-animation-duration: 16s;
  animation-duration: 16s;
  opacity: 1;
  -webkit-transform: scale(1.2);
  transform: scale(1.2);
}

.slidershow--image:nth-child(1) {
  -webkit-animation-name: kenburns-1;
  animation:16s kenburns-1 infinite linear;
  z-index: 3;
}

.slidershow--image:nth-child(2) {
  -webkit-animation-name: kenburns-2;
  animation:16s kenburns-2 infinite linear;
  z-index: 2;
}

.slidershow--image:nth-child(3) {
  -webkit-animation-name: kenburns-3;
  animation:16s kenburns-3 infinite linear;
  z-index: 1;
}

.slidershow--image:nth-child(4) {
  -webkit-animation-name: kenburns-4;
  animation:16s kenburns-4 infinite linear;
  z-index: 0;
}

@-webkit-keyframes kenburns-1 {
  0% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  1.5625% {
    opacity: 1;
  }
  23.4375% {
    opacity: 1;
  }
  26.5625% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  98.4375% {
    opacity: 0;
    -webkit-transform: scale(1.21176);
    transform: scale(1.21176);
  }
  100% {
    opacity: 1;
  }
}

@keyframes  kenburns-1 {
  0% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  1.5625% {
    opacity: 1;
  }
  23.4375% {
    opacity: 1;
  }
  26.5625% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  98.4375% {
    opacity: 0;
    -webkit-transform: scale(1.21176);
    transform: scale(1.21176);
  }
  100% {
    opacity: 1;
  }
}

@-webkit-keyframes kenburns-2 {
  23.4375% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  26.5625% {
    opacity: 1;
  }
  48.4375% {
    opacity: 1;
  }
  51.5625% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
}

@keyframes kenburns-2 {
  23.4375% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  26.5625% {
    opacity: 1;
  }
  48.4375% {
    opacity: 1;
  }
  51.5625% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
}

@-webkit-keyframes kenburns-3 {
  48.4375% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  51.5625% {
    opacity: 1;
  }
  73.4375% {
    opacity: 1;
  }
  76.5625% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
}

@keyframes kenburns-3 {
  48.4375% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  51.5625% {
    opacity: 1;
  }
  73.4375% {
    opacity: 1;
  }
  76.5625% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
}

@-webkit-keyframes kenburns-4 {
  73.4375% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  76.5625% {
    opacity: 1;
  }
  98.4375% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}

@keyframes  kenburns-4 {
  73.4375% {
    opacity: 1;
    -webkit-transform: scale(1.2);
    transform: scale(1.2);
  }
  76.5625% {
    opacity: 1;
  }
  98.4375% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}
</style>